feat: Add utility method to auto deserialize pipeline inputs - #12093
feat: Add utility method to auto deserialize pipeline inputs#12093sjrl wants to merge 7 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||
|
@sjrl That's a great addition! |
That could be a consideration, but then its starting to feel more like this util function might make more sense to have in hayhooks than in Haystack. 🤔 Would you be fine with that? |
mpangrazzi
left a comment
There was a problem hiding this comment.
Looks good! I've left two minor comments
| if _is_union_type(type_): | ||
| for arm in get_args(type_): | ||
| resolved = _resolve_coercible_class(arm) | ||
| if resolved is not None: | ||
| return resolved | ||
| return None |
There was a problem hiding this comment.
This seems to always pick the first deserializable union member. Could we get the correct member from the payload instead (and raise if not found)? Otherwise an ExtractedAnswer may be converted to GeneratedAnswer (didn't test it)
| :returns: The deserialized instance. | ||
| """ | ||
| if hasattr(target_class, "from_dict"): | ||
| return target_class.from_dict(value) |
There was a problem hiding this comment.
I may be wrong, but Tool.from_dict() mutates nested fields in this dictionary, so passing value directly modifies the caller’s payload. Maybe a deepcopy(value) would fix this? (not sure it's valuable though)
There was a problem hiding this comment.
Hmm for now I'd leave it up to the caller to the deepcopy and if need be we can open an issue in Haystack about if designing from_dict to not mutate in place. Although I believe that was a design choice to often deserialize in place.
|
Closing in favor of deepset-ai/hayhooks#252 |
Related Issues
Proposed Changes:
Added the
coerce_pipeline_inputsutility function (importable fromhaystack). It deserializes serialized Haystack objects in pipeline input data based on the pipeline's input socket types: for every provided value whose socket type involves a class exposingfrom_dict(such asChatMessageorDocument), plain dictionaries are converted into instances of that class. Socket types of the formT,list[T], and optionals/unions of those are supported, and both the nested ({"component_name": {"input_name": value}}) and flat ({"input_name": value}) input formats are accepted. This removes the need for API layers to guess which pipeline inputs contain serializedChatMessageobjects before callingPipeline.run:How did you test it?
Notes for the reviewer
Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.